//
//  BankCenterView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/9/10.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol BankCenterViewDelegate {
    @objc optional func selectCardType(title:String,imageurl:String) //选择信用卡
    @objc optional func selectBankType() //选择银行类型
    @objc optional func moreBankCard(title:String) //更多信用卡
}

class BankCenterView: UIView {
    
    weak var centerDelegate : BankCenterViewDelegate?
    fileprivate let cellId = "bankCenterCell"
    
    //信用卡
    let bankArray = [CITICBank,PufaBank,GuangfaBank,EverbrightBank,PeaceBank,CitiBank,GuangzhouBank ,ZheshangBank]
    //更多信用卡
    let moreBankArray = [CITICBank,PufaBank,GuangfaBank,EverbrightBank,PeaceBank,CitiBank,GuangzhouBank,ZheshangBank,ChinaMerchantsBank,BankofCommunications,MinshengBank,IndustrialBank,HSBCBank,ShanghaiBank,BankofChina,ABC,ICBC,Constructionbank,Postalsaving,BaoshangBank,BankofBeijing,ChongqingBank,HSBC,StandardCharteredBank]
    
    //图片
    let images = ["zhongxin","pufa","guangfa","guangda","pingan","huaqi","guangzhou","zheshang","zhaoshang","jiaotong","minsheng","xingye","huaxia","shanghai","zhongguo","nongye","gongshang","jianshe","youzheng","baoshang","beijing","chongqing","huifeng","zhada"]
    
    var dataArray = [String]() //当前数据
    
    var bankType = 1 //选中银行类型
    
    let moreButton = UIButton() //查看更多
    let centerBtn = UIButton() //热门银行中心
    let applicationBtn = UIButton() //热门银行申请
    let moreBankBtn = UIButton() //更多银行信用卡申请
    
    let grayView = UIView() //黑色线条
    let lineView = UIView() //选中标签
    
    var collectionView : UICollectionView! //银行类型

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        moreButton.setTitle(loadHard + "...", for: .normal)
        moreButton.backgroundColor = UIColor.mineColor()
        moreButton.setTitleColor(UIColor.black, for: .normal)
        moreButton.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        self.addSubview(moreButton)
        
        applicationBtn.setTitle(popularBank + cardApplication, for: .normal)
        applicationBtn.setTitleColor(UIColor.red, for: .normal)
        applicationBtn.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        applicationBtn.tag = 1
        applicationBtn.addTarget(self, action: #selector(selectBankType(sender:)), for: .touchUpInside)
        self.addSubview(applicationBtn)
        
        centerBtn.setTitle(popularBank + cardCenter, for: .normal)
        centerBtn.setTitleColor(UIColor.black, for: .normal)
        centerBtn.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        centerBtn.tag = 2
        centerBtn.addTarget(self, action: #selector(selectBankType(sender:)), for: .touchUpInside)
        self.addSubview(centerBtn)
        
        grayView.backgroundColor = UIColor.lineColor()
        self.addSubview(grayView)
        
        lineView.backgroundColor = UIColor.red
        self.addSubview(lineView)
        
        moreBankBtn.backgroundColor = UIColor.mineColor()
        moreBankBtn.setTitle(moreBankCardApplication, for: .normal)
        moreBankBtn.setTitleColor(UIColor.black, for: .normal)
        moreBankBtn.titleLabel?.font = UIFont.systemFont(ofSize: RATIO(maxNum: 16))
        moreBankBtn.addTarget(self, action: #selector(moreBankCard(sender:)), for: .touchUpInside)
        self.addSubview(moreBankBtn)
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = CGSize.init(width: ScreenW/2, height: RATIO_H(maxNum: 40))
        collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.isScrollEnabled = false
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(TypeCell.self, forCellWithReuseIdentifier: cellId)
        self.addSubview(collectionView)
        
        moreButton.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        applicationBtn.snp.makeConstraints { (make) in
            make.top.equalTo(moreButton.snp.bottom)
            make.left.equalToSuperview()
            make.width.equalTo(ScreenW/2)
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        centerBtn.snp.makeConstraints { (make) in
            make.top.equalTo(applicationBtn)
            make.right.equalToSuperview()
            make.width.equalTo(applicationBtn)
            make.height.equalTo(applicationBtn)
        }
        
        grayView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalTo(centerBtn)
            make.height.equalTo(1)
        }
        
        lineView.snp.makeConstraints { (make) in
            make.centerX.equalTo(applicationBtn)
            make.bottom.equalTo(applicationBtn)
            make.height.equalTo(grayView)
            make.width.equalTo(ScreenW/2 - RATIO(maxNum: 20))
        }
        
        moreBankBtn.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.bottom.equalToSuperview().offset(RATIO_H(maxNum: -10))
            make.height.equalTo(RATIO_H(maxNum: 30))
        }
        
        collectionView.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.top.equalTo(grayView.snp.bottom)
            make.bottom.equalTo(moreBankBtn.snp.top)
        }
        
        self.dataArray = bankArray
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: 选择银行类型
    @objc fileprivate func selectBankType(sender:UIButton) {
        
        if sender.tag == self.bankType {
            return
        }
        
        lineView.snp.remakeConstraints { (make) in
            make.centerX.equalTo(sender)
            make.bottom.equalTo(sender)
            make.height.equalTo(grayView)
            make.width.equalTo(ScreenW/2 - RATIO(maxNum: 20))
        }
        
        if sender.tag == 1 {
            applicationBtn.setTitleColor(UIColor.red, for: .normal)
            centerBtn.setTitleColor(UIColor.black, for: .normal)
            self.bankType = 1
            moreBankBtn.setTitle(moreBankCardApplication, for: .normal)
        }else{
            applicationBtn.setTitleColor(UIColor.black, for: .normal)
            centerBtn.setTitleColor(UIColor.red, for: .normal)
            self.bankType = 2
            moreBankBtn.setTitle(moreBankCardCenter, for: .normal)
        }
        
        self.dataArray = bankArray
        self.collectionView.reloadData()
        self.centerDelegate?.selectBankType!()
        
    }
    
    //MARK: 更多信用卡
    @objc fileprivate func moreBankCard(sender:UIButton) {
        
        if sender.titleLabel?.text == bankCardApplication {
            sender.setTitle(moreBankCardApplication, for: .normal)
            self.dataArray = bankArray
        }else if sender.titleLabel?.text == moreBankCardApplication {
            sender.setTitle(bankCardApplication, for: .normal)
            self.dataArray = moreBankArray
        }else if sender.titleLabel?.text == moreBankCardCenter {
            sender.setTitle(bankCardCenter, for: .normal)
            self.dataArray = moreBankArray
        }else if sender.titleLabel?.text == bankCardCenter {
            sender.setTitle(moreBankCardCenter, for: .normal)
            self.dataArray = bankArray
        }
        
        self.collectionView.reloadData()
        self.centerDelegate?.moreBankCard!(title: (sender.titleLabel?.text)!)
        
    }

}

extension BankCenterView : UICollectionViewDelegate,UICollectionViewDataSource {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? TypeCell
        
        let type = self.bankType == 1 ? cardApplication : cardCenter
        cell?.titleLabel.text = dataArray[indexPath.row] + type
        return cell!
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.centerDelegate?.selectCardType!(title: dataArray[indexPath.row], imageurl: images[indexPath.row])
    }
    
}
